/*-----------------------------------------------------------------------------
 * @file   hal_i2c.h
 * @brief  interface for I2C module operations
 * @author WLH<wanglehui8357@163.com> Copyright (C) 2021~2099.
 ----------------------------------------------------------------------------*/
#ifndef HAL_I2C_H
#define HAL_I2C_H

#include "globaldef.h"

BEGIN_DECLS

/*-----------------------------------------------------------------------------
 * I2C Controller Instance ID
-----------------------------------------------------------------------------*/
#define HAL_I2C0     0x00u
#define HAL_I2C1     0x01u
#define HAL_I2C2     0x02u
#define HAL_I2C3     0x03u
#define HAL_I2C4     0x04u
#define HAL_I2C_NULL 0xFFu

/**
 * @brief I2C speed modes
 */
typedef enum
{
    I2C_CLOCK_STANDARD = 100000u,
    I2C_CLOCK_FAST = 400000u,
    I2C_CLOCK_FAST_PLUS = 1000000u,
    I2C_CLOCK_HIGH = 3400000u,
} i2c_clock_speed;

#define IS_I2C_SPEED_CLOCK(a) (((a) == I2C_CLOCK_STANDARD) ||  \
                               ((a) == I2C_CLOCK_FAST) ||      \
                               ((a) == I2C_CLOCK_FAST_PLUS) || \
                               ((a) == I2C_CLOCK_HIGH))

/*-----------------------------------------------------------------------------
 * public functions declaration
-----------------------------------------------------------------------------*/
/**
 * @brief      init i2c controller
 *
 * @param[in]  port_num The port number
 * @param[in]  clk_hz  The clock speed
 */
bool hal_i2c_init(const U8 port_num, i2c_clock_speed clk_hz);
/**
 * @brief      get supported i2c controller count
 */
U8 hal_i2c_count(void);
/**
 * @brief      Read multi bytes from slave device
 *
 * @param[in]  port_num  The port number
 * @param[in]  addr     7 bit slave addr, left aligned, bits 7:1 are used, LSB bit is not used
 * @param[in]  reg      The register to read from, if set 0xFF, ignore it
 * @param      data     pointer to data array to store data from slave
 * @param[in]  count    how many bytes will be read
 */
bool hal_i2c_read_data(const U8 port_num, U8 addr, U8 reg, U8* data, U16 count);
/**
 * @brief      Write multi bytes from slave
 *
 * @param[in]  port_num  The I2C port number used
 * @param[in]  addr  7 bit slave addr, left aligned, bits 7:1 are used, LSB bit is not used
 * @param[in]  reg   The register to write to, if set 0xFF, ignore it
 * @param[in]  data  pointer to data array to write it to slave
 * @param[in]  count how many bytes will be written
 */
bool hal_i2c_write_data(const U8 port_num, U8 addr, U8 reg, U8* data, U16 count);

END_DECLS

#endif //~HAL_I2C_H
